package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/depts")
public class DeptController {
    @Autowired
    private DeptService deptService;

    @GetMapping
    public Result deptFindAll(){
        List<Dept> all = deptService.findAll();
        System.out.println(all);
        return Result.success(all);
    }
    @PostMapping
    public Result saveDept(@RequestBody Dept dept){
        deptService.saveDept(dept.getName());
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result findDeptById(@PathVariable("id") Integer id){
        Dept dept = deptService.findDeptById(id);
        return Result.success(dept);
    }
    @PutMapping
    public Result updateDeptById(@RequestBody Dept dept){
        deptService.updateDeptById(dept);
        return Result.success();
    }
    @DeleteMapping
    public Result deleteDeptById(@RequestParam("id") Integer id){
        deptService.deleteById(id);
        return Result.success();
    }
}
